invoiceSchema.pre(ꞌsaveꞌ)   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 12
Ratio 100 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
c 2
b 0
f 0
nc 2
nop 1
dl 12
loc 12
rs 9.4285
1 View Code Duplication
var mongoose = require('mongoose')
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
2
var invoiceItem = require('./common/line-item')
3
4
var invoiceSchema = new mongoose.Schema({
5
  date: { type: Date, default: Date.now },
6
  customer: { type: mongoose.Schema.Types.ObjectId, ref: 'Customer', required: true },
7
  items: [invoiceItem],
8
  totalVat: { type: Number, default: 0.00 },
9
  total: { type: Number, default: 0.00 }
10
})
11
12
invoiceSchema.pre('save', function (next) {
13
  let overallTotal = 0.00
14
  // let overallVat
15
  // All the items should give the total
16
  for (let item in this.items) {
17
    overallTotal += item.total
18
    // overallVat += item.subTotal * item.vatRate
19
  }
20
21
  this.total = overallTotal
22
  next()
23
})
24
25
module.exports = mongoose.model('Invoice', invoiceSchema)
26